{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('14.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<div>\n",
       " <p>\n",
       " <a href=\"1\">1</a>\n",
       " <a href=\"2\">2</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"3\">3</a>\n",
       " <a href=\"4\">4</a>\n",
       " <a href=\"5\">5</a>\n",
       " </p>\n",
       " </div>, <div>\n",
       " <p>\n",
       " <a href=\"6\">6</a>\n",
       " <a href=\"7\">7</a>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"8\">8</a>\n",
       " <a href=\"9\">9</a>\n",
       " <a href=\"10\">10</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " </div>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('div')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<div>\n",
       " <p>\n",
       " <a href=\"1\">1</a>\n",
       " <a href=\"2\">2</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"3\">3</a>\n",
       " <a href=\"4\">4</a>\n",
       " <a href=\"5\">5</a>\n",
       " </p>\n",
       " </div>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 指定某一个div标签\n",
    "sp.select('div:nth-of-type(1)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a href=\"1\">1</a>,\n",
       " <a href=\"2\">2</a>,\n",
       " <a href=\"1.5\">1.5</a>,\n",
       " <a href=\"2.5\">2.5</a>,\n",
       " <a href=\"3\">3</a>,\n",
       " <a href=\"4\">4</a>,\n",
       " <a href=\"5\">5</a>,\n",
       " <a href=\"6\">6</a>,\n",
       " <a href=\"7\">7</a>,\n",
       " <a href=\"8\">8</a>,\n",
       " <a href=\"9\">9</a>,\n",
       " <a href=\"10\">10</a>,\n",
       " <a href=\"8.5\">8.5</a>,\n",
       " <a href=\"9.5\">9.5</a>,\n",
       " <a href=\"10.5\">10.5</a>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a href=\"1\">1</a>,\n",
       " <a href=\"1.5\">1.5</a>,\n",
       " <a href=\"3\">3</a>,\n",
       " <a href=\"6\">6</a>,\n",
       " <a href=\"8\">8</a>,\n",
       " <a href=\"8.5\">8.5</a>]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('a:nth-of-type(1)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select(\"a:nth-of-type(15)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a href=\"1\">1</a>,\n",
       " <a href=\"2\">2</a>,\n",
       " <a href=\"3\">3</a>,\n",
       " <a href=\"4\">4</a>,\n",
       " <a href=\"5\">5</a>,\n",
       " <a href=\"6\">6</a>,\n",
       " <a href=\"7\">7</a>,\n",
       " <a href=\"8\">8</a>,\n",
       " <a href=\"9\">9</a>,\n",
       " <a href=\"10\">10</a>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('div > p > a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a href=\"1\">1</a>, <a href=\"2\">2</a>, <a href=\"6\">6</a>, <a href=\"7\">7</a>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('div > p:nth-of-type(1) > a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a href=\"2\">2</a>, <a href=\"7\">7</a>]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 按组来分的\n",
    "sp.select('div > p:nth-of-type(1) > a:nth-of-type(2)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<html lang=\"en\">\n",
       " <head>\n",
       " <meta charset=\"utf-8\"/>\n",
       " <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       " <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       " <title>CSS选择器语法</title>\n",
       " </head>\n",
       " <body>\n",
       " <h1>h1</h1>\n",
       " <h2>h2</h2>\n",
       " <div>\n",
       " <p>\n",
       " <a href=\"1\">1</a>\n",
       " <a href=\"2\">2</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"3\">3</a>\n",
       " <a href=\"4\">4</a>\n",
       " <a href=\"5\">5</a>\n",
       " </p>\n",
       " </div>\n",
       " <div>\n",
       " <p>\n",
       " <a href=\"6\">6</a>\n",
       " <a href=\"7\">7</a>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"8\">8</a>\n",
       " <a href=\"9\">9</a>\n",
       " <a href=\"10\">10</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " </div>\n",
       " <footer>\n",
       "         底部信息1\n",
       "     </footer>\n",
       " <footer>\n",
       "         底部信息2\n",
       "     </footer>\n",
       " <footer>\n",
       "         底部信息3\n",
       "     </footer>\n",
       " </body>\n",
       " </html>, <head>\n",
       " <meta charset=\"utf-8\"/>\n",
       " <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       " <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       " <title>CSS选择器语法</title>\n",
       " </head>, <meta charset=\"utf-8\"/>, <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>, <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>, <title>CSS选择器语法</title>, <body>\n",
       " <h1>h1</h1>\n",
       " <h2>h2</h2>\n",
       " <div>\n",
       " <p>\n",
       " <a href=\"1\">1</a>\n",
       " <a href=\"2\">2</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"3\">3</a>\n",
       " <a href=\"4\">4</a>\n",
       " <a href=\"5\">5</a>\n",
       " </p>\n",
       " </div>\n",
       " <div>\n",
       " <p>\n",
       " <a href=\"6\">6</a>\n",
       " <a href=\"7\">7</a>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"8\">8</a>\n",
       " <a href=\"9\">9</a>\n",
       " <a href=\"10\">10</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " </div>\n",
       " <footer>\n",
       "         底部信息1\n",
       "     </footer>\n",
       " <footer>\n",
       "         底部信息2\n",
       "     </footer>\n",
       " <footer>\n",
       "         底部信息3\n",
       "     </footer>\n",
       " </body>, <h1>h1</h1>, <h2>h2</h2>, <div>\n",
       " <p>\n",
       " <a href=\"1\">1</a>\n",
       " <a href=\"2\">2</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"3\">3</a>\n",
       " <a href=\"4\">4</a>\n",
       " <a href=\"5\">5</a>\n",
       " </p>\n",
       " </div>, <a href=\"1\">1</a>, <a href=\"2\">2</a>, <span>\n",
       " <p>\n",
       " <a href=\"1.5\">1.5</a>\n",
       " <a href=\"2.5\">2.5</a>\n",
       " </p>\n",
       " </span>, <a href=\"1.5\">1.5</a>, <a href=\"2.5\">2.5</a>, <a href=\"3\">3</a>, <a href=\"4\">4</a>, <a href=\"5\">5</a>, <div>\n",
       " <p>\n",
       " <a href=\"6\">6</a>\n",
       " <a href=\"7\">7</a>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"8\">8</a>\n",
       " <a href=\"9\">9</a>\n",
       " <a href=\"10\">10</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " </div>, <a href=\"6\">6</a>, <a href=\"7\">7</a>, <a href=\"8\">8</a>, <a href=\"9\">9</a>, <a href=\"10\">10</a>, <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>, <a href=\"8.5\">8.5</a>, <a href=\"9.5\">9.5</a>, <a href=\"10.5\">10.5</a>, <footer>\n",
       "         底部信息1\n",
       "     </footer>, <footer>\n",
       "         底部信息2\n",
       "     </footer>, <footer>\n",
       "         底部信息3\n",
       "     </footer>]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 取反（not）操作\n",
    "sp.select('*:not(p)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
